//
//  LDSCollectionViewCellViewModel.m
//  LDSMVVMBase
//
//  Created by kai_leedarson on 2017/10/9.
//  Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSCollectionViewCellViewModel.h"

@implementation LDSCollectionViewCellViewModel

- (LDSBaseCollectionViewCell *)cell {
    if (_viewClass && [_viewClass isSubclassOfClass:[LDSBaseCollectionViewCell class]]) {
        NSString *identifier = [NSString stringWithFormat:@"cell:<%@>",NSStringFromClass(_viewClass)];
        LDSBaseCollectionViewCell *cell = nil;
        if (_collectionView) {
            NSString *xibPath = [[NSBundle mainBundle] pathForResource:NSStringFromClass(_viewClass) ofType:@"nib"];
            if([[NSFileManager defaultManager] fileExistsAtPath:xibPath]){
                UINib *nib = [UINib nibWithNibName:NSStringFromClass(_viewClass) bundle:nil];
                [_collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
            } else {
                [_collectionView registerClass:_viewClass forCellWithReuseIdentifier:identifier];
            }
            cell = [_collectionView dequeueReusableCellWithReuseIdentifier:identifier
                                                              forIndexPath:self.indexPath];
            if (cell) {
                cell.tag = self.tag;
            }
        }
        return cell;
    }
    return nil;
}

+ (LDSCollectionViewCellViewModel *)modelCellWithClass:(Class)vClass model:(id)model{
    return [LDSCollectionViewCellViewModel modelCellWithClass:vClass model:model tag:-1];
}

+ (LDSCollectionViewCellViewModel *)modelCellWithClass:(Class)vClass
                                                 model:(id)model
                                                   tag:(NSInteger)tag{
    LDSCollectionViewCellViewModel *vModel = [[LDSCollectionViewCellViewModel alloc] init];
    if (vClass) {
        [vModel setViewClass:vClass];
        [vModel setModel:model];
        [vModel setTag:tag];
    }
    return vModel;
    
}
@end
